// 导入路由相关的模块和类型
import { RouteLocationNormalized, RouteRecordRaw, createRouter, createWebHashHistory } from "vue-router";

// 定义路由配置
export const constantRoutes: RouteRecordRaw[] = [
    {
        path: '/',
        name: 'index',
        meta: { requiresAuth: false },
        component: () => import("../components/layout/layout.vue"),
        redirect: "/dashboard",
        children: [
            {
                path: "/dashboard",
                name: "dashboard",
                component: () => import("../components/index/dashboard.vue"),
                meta: { requiresAuth: true } // 修改为 true 表示需要登录
            },
            {
                path: '/store',
                name: 'store',
                meta: { requiresAuth: true }, // 修改为 true 表示需要登录
                children: [
                    {
                        path: "store_info",
                        name: "store_info",
                        component: () => import('../components/store/storeInfo.vue'),
                        meta: { requiresAuth: true },
                    },
                    {
                        path: "store_list",
                        name: "store_list",
                        component: () => import('../components/store/storeList.vue'),
                        meta: { requiresAuth: true },
                    },
                    {
                        path: "store_sale",
                        name: "store_sale",
                        component: () => import('../components/store/storeSale.vue'),
                        meta: { requiresAuth: true },
                    }
                    // 其他子路由
                ]
            },
            {
                path: '/user',
                name: 'user',
                meta: { requiresAuth: true }, // 修改为 true 表示需要登录
                children: [
                    {
                        path: "info",
                        name: "info",
                        component: () => import('../components/userInfo.vue'),
                        meta: { requiresAuth: true },
                    },
                    {
                        path: "password",
                        name: "password",
                        component: () => import('../components/user/changePassword.vue'),
                        meta: { requiresAuth: true },
                    },
                    {
                        path: "avatar",
                        name: "avatar",
                        component: () => import('../components/user/changeAvatar.vue'),
                        meta: { requiresAuth: true },
                    }
                    // 其他子路由
                ]
            },
            {
                path: '/vip',
                name: 'vip',
                meta: { requiresAuth: true }, // 修改为 true 表示需要登录
                children: [
                    {
                        path: "vip_list",
                        name: "vip_list",
                        component: () => import('../components/vip/vipList.vue'),
                        meta: { requiresAuth: true },
                    },
                    {
                        path: "vip_sale",
                        name: "vip_sale",
                        component: () => import('../components/vip/vipSpendPower.vue'),
                        meta: { requiresAuth: true },
                    }
                ]
            }
            // 其他路由配置保持不变
        ]
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('../components/login.vue'),
        meta: { requiresAuth: false },
    },
    {
        path: '/register',
        name: 'register',
        component: () => import('../components/register.vue'),
        meta: { requiresAuth: false },
    },
    {
        path: "/logout",
        name: "logout",
        component: () => import("../components/logout.vue"),
        meta: { requiresAuth: false },
    },
    {
        path: "/404",
        name: "404",
        component: () => import("../components/unfind.vue"),
        meta: { requiresAuth: false },
    },
    // 空路径匹配所有其他情况
    {
        path: '/:catchAll(.*)',
        redirect: '/404',
    },
    // 其他路由配置保持不变
];

// 创建自定义路由实例
const createCustomRouter = () => createRouter({
    history: createWebHashHistory(),
    routes: constantRoutes,
});

// 初始化路由
let router = createCustomRouter();

// 重置路由函数
export function resetRouter() {
    const newRouter = createCustomRouter();
    router = newRouter;
}

// 全局前置守卫
router.beforeEach((to: RouteLocationNormalized, _, next) => {
    // 获取目标路由的 meta 信息
    const requiresAuth = to.meta.requiresAuth;

    // 如果目标路由需要登录验证，且用户未登录，则重定向到登录页面
    if (requiresAuth && !checkUserAuthentication()) {
        next("/login");
    } else {
        next();
    }
});

// 全局后置钩子
router.afterEach(() => {
    // 可以在这里执行一些操作，例如页面埋点等
});

// 实际的身份验证逻辑，根据实际情况填充这个函数
function checkUserAuthentication(): boolean {

    let isLogin = false;

    const sessionID = document.cookie.includes("sessionId");

    const isLocalLogin = sessionID != null ? false : true;


    // 远程获取该用户是否登录 以远程为准

    // 一种是向后端请求接口用接口判断是否登录
    // 另一种是使用 cookie 等本地存储判断是否登录
    // 下面使用 cookie 模拟用户登录
    return isLogin
}

// 导出路由实例
export default router;